package com.rao.travel.controller;

import com.rao.travel.pojo.Permission;
import com.rao.travel.pojo.Role;
import com.rao.travel.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className RoleController
 * @date 2019/11/5 9:59
 * @package com.rao.travel.controller
 * @description
 */

@RequestMapping("role")
@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    /*查询所有角色*/
    @RequestMapping("findAll.do")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList = roleService.findAll();
        modelAndView.addObject("roleList", roleList);
        modelAndView.setViewName("role-list");
        return modelAndView;
    }

    /*保存角色*/
    @RequestMapping("save.do")
    public String save(Role role){
        roleService.save(role);
        return "redirect:findAll.do";
    }

    /*查询角色详情*/
    @RequestMapping("findById.do")
    public ModelAndView findById(@RequestParam(name = "id") String id){
        ModelAndView modelAndView = new ModelAndView();
        Role role = roleService.findById(id);
        modelAndView.addObject("role", role);
        modelAndView.setViewName("role-show");
        return modelAndView;
    }

    /*删除角色*/
    @RequestMapping("deleteById.do")
    public String deleteById(@RequestParam(name = "id")String id){
        roleService.deleteById(id);
        return "redirect:findAll.do";
    }

    /*跳转到添加权限界面*/
    @RequestMapping("findRoleByIdAndAllPermission.do")
    public ModelAndView findRoleByIdAndAllPermission(@RequestParam(name = "id")String id){
        ModelAndView modelAndView = new ModelAndView();
        Role role = roleService.findById(id);
        List<Permission> permissionList = roleService.findOtherPermission(id);
        modelAndView.addObject("role", role);
        modelAndView.addObject("permissionList", permissionList);
        modelAndView.setViewName("role-permission-add");
        return modelAndView;
    }

    /*为角色添加权限*/
    @RequestMapping("addPermissionToRole.do")
    public String addPermissionToRole(@RequestParam(name = "roleId")String roleId,
                                      @RequestParam(name = "ids")String[] ids){

        roleService.addPermissionToRole(roleId, ids);
        return "redirect:findAll.do";
    }

}
